<template>
	<view>
		<billPicker ref="refBillPicker" v-model="bill" title="盘点单" @confirm="onConfirmBill" :rangeSource="{ apiMethod: 'cimasterpage', params: {} }"></billPicker>

		<view class="cu-form-group">
			<view class="title">盘点周期</view>
			<input placeholder="" :disabled="true" v-model="cycle" name="input" />
		</view>
		
		<view class="cu-form-group ">
			<view class="title">盘点范围</view>
			<input placeholder="" :disabled="true" v-model="scope" name="input" />
		</view>
		
		<todo-input v-model="todoNum"  @confirm="onConfirmTodo" :focus="todoFocus" @focusChange="onTodoFocusChange"></todo-input>

		<view class="cu-bar bg-white solid-bottom margin-top">
			<view class="action">
				<text class="cuIcon-title text-orange"></text>
				清单列表
			</view>
		</view>
		<!-- todolist -->
		<todo-list-view class="cu-list menu" :top="560">
			<view v-for="(item, idx) in todoList" class="cu-item">
				<view class="content padding-tb-sm">
					<view>
						<text class="cuIcon-formfill text-blue margin-right-xs"></text>
						<text>发料单:</text>
						{{ item.bill_id }}
					</view>
					<view class="text-gray text-sm">
						<text class="lg text-gray cuIcon-settingsfill"></text>
						<text>料号:</text>
						{{ item.part_id }}
					</view>
				</view>
				<view class="action">
					<view class="text-grey text-xs">
						<text class="text-gray">开单数量({{ item.inventory_qty }})</text>
						<!-- <text class="text-gray">需求({{ item.plan_qty }})</text> -->
					</view>
					<view class="text-gray  text-xs">
						<!-- <text class="text-gray">本次({{ item.current_qty }})</text> -->
						<text class="text-gray">盘点数量({{ item.ci_qty }})</text>
					</view>
				</view>
			</view>
		</todo-list-view>

		<view class="cu-bar tabbar bg-white shadow foot foot-btn-group">
			<button class="cu-btn bg-white" @tap="onCancelDB">取消盘点</button>
			<button class="cu-btn bg-white" @tap="onCloseDB">盘点完成</button>
		</view>

		<bill-prompt ref="refCancelPrompt" @confirm="onCancelConfirm" title="取消盘点"></bill-prompt>
	</view>
</template>

<script>
import billPicker from './bill-picker.vue';
import billPrompt from './bill-prompt.vue';
import todoInput from './todo-input';
import todoListView from './todo-list-view.vue'
import util from '@/serveices/util.js'
import api from '@/serveices/wms-api.js';
export default {
	components: { billPicker, billPrompt ,todoInput,todoListView},
	data() {
		return {
			billInfo: null,
			bill: '',
			cycle: '', //盘点周期
			scope: '', //盘点范围
			todoNum: '',
			todoFocus:true,
			todoList: []
		};
	},
	methods: {
		onTodoFocusChange(focus){
			this.todoFocus = focus
		},
		onConfirmBill({ bill },isPlayVoice) {
			this.bill = bill
			return api.cidetail(this.bill).then(res => {
				this.todoList = res.data;
				if (res.data.length) {
					this.cycle = res.data[0].ci_cycle;
					this.scope = res.data[0].ci_scope;
					// this.$refs.refBillPicker.confirmBill(this.bill);
					this.$refs.refBillPicker.lock()
					if(isPlayVoice) util.playSuccess();
				}else{
					this.$refs.refBillPicker.clear()
					if(isPlayVoice) util.playFailed();
				}
				this.todoFocus = true
			}).catch(err=>{
				//this.$refs.refBillPicker.confirmBill(this.bill,false)
				this.$refs.refBillPicker.clear()
				if(isPlayVoice) util.playFailed();
			})
		},
		onConfirmTodo() {
			if (!this.bill) {
				// 3.1.	若调拨单文本框为空，此时扫描内容作为调拨单处理
				this.onConfirmBill({ bill: this.todoNum },true).then(res => {
					this.todoNum = '';
				});
			} else {
				//3.3.	若调拨单号和储位文本框均不为空，扫描内容作为物料信息处理
				api.cioperation({
					opType: 'CI',
					billNo: this.bill,
					scanInfo: this.todoNum
				}).then(res => {
					this.todoList = res.data;
					this.todoNum = ''
					this.playSuccess()
				}).catch(err=>{
					this.todoNum = ''
					util.playFailed()
				})
			}
		},
		onCancelDB() {
			this.$refs.refCancelPrompt.show();
		},
		onCancelConfirm(scanInfo) {
			api.cioperation({
				opType: 'CANCEL',
				billNo: '',
				scanInfo: scanInfo
			}).then(res => {
				//刷新单据
				if (this.bill) {
					// this.buildDetail();
					this.onConfirmBill({bill:this.bill})
				}
				util.playSuccess()
			}).catch(err=>{
				util.playFailed()
			}).finally(r=>{
				this.todoFocus = true
			})
		},
		onCloseDB() {
			//需检查下方表格中是否存在扫描数量与需求数量不相等的情况，若有，提示还有物料未扫满，若均相等
		  uni.showModal({
		  	title: '系统提示',
		  	content: '是否确认盘点完成？',
		  	success: res => {
		  		api.cioperation({
		  			opType: 'CLOSED',
		  			billNo: this.bill,
		  			scanInfo: ''
		  		}).then(res => {
		  			this.clearForm();
		  		});
		  	}
		  });
		},

		clearForm() {
			this.bill = '';
			this.todoNum = '';
			this.scope = '';
			this.cycle = '';
			this.todoList = [];
			this.$refs.refBillPicker.clear()
			this.todoFocus = true
		}
	}
};
</script>

<style></style>
